package campeonatofutebol.activity;


import java.util.ArrayList;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import campeonatofutebol.util.CarregandoFeedsTask;

import com.example.campeonatofutebol.R;

@SuppressLint("DefaultLocale")
public class JogosList extends ListActivity {

	private ListView list;
	private ArrayList<String> conteudos;
	private CarregandoFeedsTask c;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pesquisa_layout);
		setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL) ;
		list = (ListView) findViewById(android.R.id.list);
		list.setTextFilterEnabled(true);
		
		c = new CarregandoFeedsTask(JogosList.this);
		String dia = getIntent().getStringExtra("diaDaSemana");
		c.setDiaDaSemana(dia);
        c.execute("");
        getListView().setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(android.widget.AdapterView<?> parent, View view,
  		          int position, long id) {
				String messageItem = (String) getListView().getItemAtPosition(position);
				Calendar c = Calendar.getInstance();
				if((messageItem.contains(" x ")) && (messageItem.contains(c.get(Calendar.DAY_OF_MONTH) + " de "))){
					Intent i = new Intent(JogosList.this, GamePage.class);
					Bundle b = new Bundle();
					b.putSerializable("tituloJogo", messageItem);
					i.putExtras(b);
					startActivity(i);
				}else{
					Toast.makeText(getApplicationContext(), "O chat só estará disponível no dia do jogo!",
		    			    Toast.LENGTH_SHORT).show();
				}
			};
		});
//        getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
//            @Override
//            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//    			Intent i = new Intent(JogosList.this, GamePage.class);
//    			i.putExtra("testeMural", true);
//				String messageItem = (String) getListView().getItemAtPosition(position);
//				Calendar c = Calendar.getInstance();
//				i.putExtra("tituloJogo", messageItem);
//    			startActivity(i);
//                return true;
//            }
//        });
        manipulaIntent(getIntent());
	}
	
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		manipulaIntent(intent);
	}

	private void manipulaIntent(Intent intent) {
	    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
	      String pesquisa = intent.getStringExtra(SearchManager.QUERY);
	      realizarPesquisa(pesquisa);
	    }
	}
	
	private void realizarPesquisa(String pesquisa) {
		ArrayList<String> resultadoPesquisa = new ArrayList<String>();
		for (int i = 0; i < conteudos.size() ; i++) {
			if(conteudos.get(i).toLowerCase().contains(pesquisa.toLowerCase())){
				resultadoPesquisa.add(conteudos.get(i));
			}
		}
		if(!resultadoPesquisa.isEmpty()){
			setListAdapter(new ArrayAdapter<String>(this, R.layout.row, resultadoPesquisa));
		}else{
			Toast.makeText(getApplicationContext(), "Nenhuma palavra-chave encontrada",
    			    Toast.LENGTH_LONG).show();
		}
	}

	@Override
	public boolean onSearchRequested() {
		
		return super.onSearchRequested();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.pesquisa_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(c.getResult()!= null){
			conteudos = (ArrayList<String>)c.getJogosDaSemana();
		}
		switch (item.getItemId()) {
			case R.id.pesquisa_item:
				onSearchRequested();
			case R.id.todos_jogos_item:
				setListAdapter(new ArrayAdapter<String>(this, R.layout.row, conteudos));
			default:
				return super.onOptionsItemSelected(item);
		}
	}
}
